﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FieldTools.Client.View.Window
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TestWindow : System.Windows.Window
    {
        CancellationTokenSource tokenSource = new();
        public TestWindow()
        {
            InitializeComponent();
            Task.Run(async () =>
            {
                //tokenSource.Token.ThrowIfCancellationRequested();
                int i = 0;
                while (true)
                {
                    await Task.Delay(1000);
                    Console.WriteLine($"线程打印测试：第{++i}次");
                    if (tokenSource.Token.IsCancellationRequested)
                    {
                        // Clean up here, then...
                        tokenSource.Token.ThrowIfCancellationRequested();
                    }
                }
            }, tokenSource.Token);
            this.Closing += TestWindow_Closing;
        }

        private void TestWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
        {
            tokenSource.Cancel();
        }
    }
}
